const usersModel = require('../models/usersModels');
module.exports.insert = async function(users){
    return await usersModel.create(users);
}


//分页
module.exports.find = async function(page,searchParams){
    console.log(searchParams)
    // return await studentModel.find(searchParams);
    // console.log(page)
    let {current,pageSize} = page;
    console.log(current,pageSize)
    // 根据查询条件获取总数
    let total = await usersModel.countDocuments(searchParams);
    // 根据查询条件获取具体数量的数据
    let data = await usersModel.find(searchParams)
                    .limit(pageSize)
                    .skip((current - 1) * pageSize)
                    .sort({"_id":-1})
    return {
        current,pageSize,total,rows:data,
    }
    
}

//根据id查找
module.exports.findById = async function(id){
    return await usersModel.findById(id);
}

module.exports.delete = async function(id){
    return await usersModel.deleteOne({_id:id});
}

module.exports.update = async function(users){
    return await usersModel.updateOne({_id:users._id},users);
}

module.exports.findOne=async function(data){
    console.log(data);
    let obj={}
    let type = data.seclectBox
    obj[type]=data.message
    console.log(obj);
    return await usersModel.find(obj).exec();
}

